package ee.android.kinokavad.adapters;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import ee.android.kinokavad.R;
import ee.android.kinokavad.data.Session;

public class MovieSessionAdapter extends BaseExpandableListAdapter{

	private List<String> dates;
	private List<ArrayList<Session>> sessions;
	private LayoutInflater inflater;

	public MovieSessionAdapter(Context context, List<String> dates,
			List<ArrayList<Session>> sessions) {
		this.dates = dates;
		this.sessions = sessions;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return sessions.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	private class ChildHolder {
		private TextView time;
		private TextView cinema;
	}

	private class GroupHolder {
		private TextView date;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {

		ChildHolder holder = null;

		if (convertView == null) {
			holder = new ChildHolder();
			convertView = inflater.inflate(R.layout.movie_session_child_row, null);
			holder.time = (TextView) convertView.findViewById(R.id.movieSessionChildRowTime);
			holder.cinema = (TextView) convertView.findViewById(R.id.movieSessionChildRowCinema);
			convertView.setTag(holder);
		} else {
			holder = (ChildHolder) convertView.getTag();
		}

		Session session = sessions.get(groupPosition).get(childPosition);
		holder.time.setText(new DateTime(session.getDateTime()).toString("HH:mm"));
		holder.cinema.setText(session.getCinemaName());
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return sessions.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return dates.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return dates.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

		GroupHolder holder = null;

		if (convertView == null) {
			holder = new GroupHolder();
			convertView = inflater.inflate(R.layout.movie_session_group_row, null);
			holder.date = (TextView) convertView.findViewById(R.id.movieSessionGroupRowDate);
			convertView.setTag(holder);
		} else {
			holder = (GroupHolder) convertView.getTag();
		}

		String date = dates.get(groupPosition);
		holder.date.setText(date);

		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return false;
	}

}
